package com.dts.backproject.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dts.backproject.entity.po.User;
import com.dts.backproject.entity.po.UserInfo;
import com.dts.backproject.exception.MyException;
import com.dts.backproject.mapper.UserInfoMapper;
import com.dts.backproject.mapper.UserMapper;
import com.dts.backproject.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserInfoServiceImpl implements UserInfoService {
    @Autowired
    private UserInfoMapper userInfoMapper;
    @Autowired
    private UserMapper userMapper;
    @Override
    public void add(UserInfo userInfo) {
        String email = userInfo.getEmail();
        LambdaQueryWrapper<User> eq = new QueryWrapper<User>().lambda().eq(User::getEmail, email);
        if(!BeanUtil.isEmpty(userMapper.selectOne(eq))){
            userInfoMapper.insert(userInfo);
        }
    }

    @Override
    public UserInfo findByEmail(String email) {
        if(email == null || email.trim().equals("")){
            throw new RuntimeException("暂无该用户信息");
        }
        LambdaQueryWrapper<User> eq = new QueryWrapper<User>().lambda().eq(User::getEmail, email);

        if(userMapper.selectOne(eq) == null){
            throw new RuntimeException("暂无该用户信息");
        }

        LambdaQueryWrapper<UserInfo> wrapper = new QueryWrapper<UserInfo>().lambda().eq(UserInfo::getEmail, email);

        UserInfo userInfo = userInfoMapper.selectOne(wrapper);
        if(userInfo == null){
            throw new RuntimeException("暂无该用户信息");
        }
        return userInfo;
    }

    @Override
    public void updateAvatarUrl(String email, String avatarUrl) {
        if(email == null || email.trim().equals("")){
            throw new RuntimeException("邮箱不能为空");
        }
        
        // 先检查用户是否存在
        LambdaQueryWrapper<User> userWrapper = new QueryWrapper<User>().lambda().eq(User::getEmail, email);
        User user = userMapper.selectOne(userWrapper);
        if(user == null){
            throw new RuntimeException("用户不存在");
        }
        
        // 查找用户信息记录
        LambdaQueryWrapper<UserInfo> wrapper = new QueryWrapper<UserInfo>().lambda().eq(UserInfo::getEmail, email);
        UserInfo userInfo = userInfoMapper.selectOne(wrapper);
        
        if(userInfo == null){
            // 如果用户信息不存在，创建新的用户信息记录
            userInfo = new UserInfo();
            userInfo.setEmail(email);
            userInfo.setAvatarUrl(avatarUrl);
            userInfoMapper.insert(userInfo);
        } else {
            // 如果用户信息存在，更新头像URL
            userInfo.setAvatarUrl(avatarUrl);
            userInfoMapper.updateById(userInfo);
        }
    }

    @Override
    public void update(UserInfo userInfo) {
        String email = userInfo.getEmail();
        LambdaQueryWrapper<UserInfo> wrapper = new LambdaQueryWrapper<UserInfo>().eq(UserInfo::getEmail, email);
        UserInfo user = userInfoMapper.selectOne(wrapper);
        if(user == null){
            throw new MyException("用户不存在！");
        }
        userInfo.setId(user.getId());
        userInfoMapper.updateById(userInfo);
    }
}

